home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Utilities / Programming / EnterAct 3.7.3 / Read Res project / Read_Res Source / Code_Main.c next >
C/C++ Source or Header  |  1995-01-13  |  9KB  |  358 lines

  1. /***********main file for code resource**************/
  2. /* Copyright © 1991, 1992 the Free Software Foundation, Inc.
  3.  *         This file is free software; you can redistribute or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 1, or any later version.
  6.  *         This file is distributed in the hope that it will be useful,
  7.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9.  * GNU General Public License for more details.
  10.  *         You should have received a copy of the GNU General Public License
  11.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  12.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  13.  * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  14.    Revised May 1992: new setup/restore a4 strategy.
  15.  */
  16. #include "AppCodeComm.h"
  17. #include <string.h>
  18.  
  19. AppCodeComm    gacc;
  20.  
  21. /* Global to indicate if calling app's event loop has been made
  22. available and should call it */
  23. Boolean gConcurrent;
  24.  
  25. #ifndef NULL
  26. #define NULL        ((void *) 0)
  27. #endif
  28.  
  29. long _code_a4, _app_a4;
  30.  
  31. #define SetUpA4()    asm { move.l a4,_temp_a }\
  32.                     asm { move.l _temp_c, a4 }\
  33.                     _app_a4 = _temp_a;
  34.                             
  35.  
  36. #define RestoreA4()        long    _temp_a, _temp_c = _code_a4;\
  37.                         asm { move.l _app_a4,a4 }
  38. /*
  39. -call RestoreA4() at very beginning of callback
  40. -at end of each callback do SetUpA4()
  41. -there's also a bit of asm at the top of main() to recover a0,
  42.     which contains the Code's a4 value, and at bottom to restore a4.
  43. */
  44.  
  45. /* "main" call for the specific code resource */
  46. extern short DoReadRes(void); /* see Read_Res.c */
  47.  
  48. /* Protos for functions in this file */
  49. void main(ACCPtr ac);
  50. /* Callbacks, to be made available to other files */
  51. short InDictionary(char *tokenName);
  52. Boolean HasInDictionary(void);
  53. Handle GetFrontText(Boolean getItAll);
  54. Boolean HasGetFrontText(void);
  55. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  56.         short *vRefNumPtr, char *fileName, Boolean clearFlag);
  57. Boolean HasGetNextMultiFile(void);
  58. short OKStopAlert(Ptr cstringPtr);
  59. Boolean HasOKStopAlert(void);
  60. void MemoryAlert(void);
  61. Boolean HasMemoryAlert(void);
  62. short GetScreenHeight(void);
  63. Boolean HasGetScreenHeight(void);
  64. short GetScreenWidth(void);
  65. Boolean HasGetScreenWidth(void);
  66. void SetWatchCursor(void);
  67. Boolean HasSetWatchCursor(void);
  68. void DoEventLoopOnce(void);
  69. Boolean HasDoEventLoopOnce(void);
  70. Handle GetTheClip(void);
  71. Boolean HasGetTheClip(void);
  72. short PutTheClip(char *newClipStr);
  73. Boolean HasPutTheClip(void);
  74.  
  75. /* The "main" that is called from the running application. This file is
  76. kept small and best in its own segment because lots of other things get piled
  77. into here as well - the jump table for the code resource, for example.
  78. So do the bare minimum - set up the global register reference, call the
  79. code resource main function, pass back a result code, and restore the
  80. a4 register. Functions below main() are the extension interface
  81. functions, which must be in this file to make use of the little functions
  82. above that save and restore a4. They all check to see that the extension
  83. in question has been passed from the application, and if not either return
  84. a zero or NULL or return a safe general value. No extension should be
  85. essential to the operation of the code resource.
  86. */
  87. /* results:
  88. <= -3 : counts as -1 at present
  89. -2 : show stderr
  90. -1 : user cancelled or error during dialog - no run
  91. 0  : run OK, do nothing special after
  92. 1  : show stdout
  93. 2  : show and select stdout
  94. > 2 : no action at present (counts as equivalent to 0)
  95. */
  96. void main(ACCPtr ac)
  97.     {
  98.     short    result;
  99.     long    _temp_a, _temp_c;
  100.     
  101.     /* initial setup of a4 */
  102.     asm
  103.         {
  104.         move.l    a4, _temp_a
  105.         move.l    a0, _temp_c
  106.         move.l    a0, a4
  107.         }
  108.     _app_a4 = _temp_a;
  109.     _code_a4 = _temp_c;
  110.     
  111.     /* Uncomment this for a version "sanity check": **************
  112.     if (ac->version < 0 || ac->version > 100)
  113.         {
  114.         OKStopAlert("Version number passed by calling application \
  115. is impossible. Please fix the application before trying again.");
  116.         ac->result = -1;
  117.         asm
  118.             {
  119.             move.l _app_a4, a4
  120.             }
  121.         return;
  122.         }
  123.     ********** end version check */
  124.     
  125.     gacc = *ac; /* make a global copy */
  126.     if (gacc.version <= 1)
  127.         {
  128.         gacc.DoEventLoopOnce_Ext = NULL;
  129.         gacc.GetAppClip_Ext = NULL;
  130.         }
  131.     if (HasDoEventLoopOnce())
  132.         gConcurrent = TRUE;
  133.     else
  134.         gConcurrent = FALSE;
  135.  
  136.     /* call 'true' main of code resource */
  137.     result = DoReadRes();
  138.     ac->result = result;
  139.     /* unload non-main segments */
  140.     UnloadA4Seg(NewPtr);
  141.     UnloadA4Seg(strcmp);
  142.     /* restore app's a4 */
  143.     asm
  144.         {
  145.         move.l _app_a4, a4
  146.         }
  147.     /* return nothing - communication via ac->result */
  148.     }
  149.  
  150. /* Extension functions. See Call_Resource.c for details
  151. on what these do. EnterAct supplies them all, but your application
  152. doesn't have to supply any of them, and your code resource should
  153. not rely on any of them being available.
  154.  
  155. A Boolean companion function for each extension reports whether
  156. the extension is available for use. 
  157. */
  158.  
  159. short InDictionary(char *tokenName)
  160.     {
  161.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  162.     short    ret;
  163.     
  164.     RestoreA4();
  165.     if (laccp->InDictionary_Ext != NULL)
  166.         ret = (*(laccp->InDictionary_Ext))(tokenName);
  167.     else
  168.         ret = 0;
  169.     SetUpA4();
  170.     return(ret);
  171.     }
  172.  
  173. Boolean HasInDictionary()
  174.     {
  175.     return(gacc.InDictionary_Ext != NULL);
  176.     }
  177.  
  178. Handle GetFrontText(Boolean getItAll)
  179.     {
  180.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  181.     Handle    ret;
  182.     
  183.     RestoreA4();
  184.     if (laccp->GetFrontText_Ext != NULL)
  185.         ret = (*(laccp->GetFrontText_Ext))(getItAll);
  186.     else
  187.         ret = NULL;
  188.     SetUpA4();
  189.     return(ret);
  190.     }
  191.  
  192. Boolean HasGetFrontText()
  193.     {
  194.     return(gacc.GetFrontText_Ext != NULL);
  195.     }
  196.  
  197. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  198.         short *vRefNumPtr, char *fileName, Boolean clearFlag)
  199.     {
  200.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  201.     
  202.     RestoreA4();
  203.     if (laccp->GetNextMultiFile_Ext != NULL)
  204.         (*(laccp->GetNextMultiFile_Ext))(panePtr, indexPtr,
  205.             vRefNumPtr, fileName, clearFlag);
  206.     else
  207.         *indexPtr = -1;
  208.     SetUpA4();
  209.     }
  210.  
  211. Boolean HasGetNextMultiFile()
  212.     {
  213.     return(gacc.GetNextMultiFile_Ext != NULL);
  214.     }
  215.  
  216. short OKStopAlert(Ptr cstringPtr)
  217.     {
  218.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  219.     short    ret;
  220.     
  221.     RestoreA4();
  222.     if (laccp->OKStopAlert_Ext != NULL)
  223.         ret = (*(laccp->OKStopAlert_Ext))(cstringPtr);
  224.     else
  225.         ret = 0;
  226.     SetUpA4();
  227.     return(ret);
  228.     }
  229.  
  230. Boolean HasOKStopAlert()
  231.     {
  232.     return(gacc.OKStopAlert_Ext != NULL);
  233.     }
  234.  
  235. void MemoryAlert()
  236.     {
  237.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  238.     
  239.     RestoreA4();
  240.     if (laccp->MemoryAlert_Ext != NULL)
  241.         (*(laccp->MemoryAlert_Ext))();
  242.     SetUpA4();
  243.     }
  244.  
  245. Boolean HasMemoryAlert()
  246.     {
  247.     return(gacc.MemoryAlert_Ext != NULL);
  248.     }
  249.  
  250. short GetScreenHeight()
  251.     {
  252.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  253.     short        ret;
  254.     
  255.     RestoreA4();
  256.     if (laccp->GetScreenHeight_Ext != NULL)
  257.         ret = (*(laccp->GetScreenHeight_Ext))();
  258.     else
  259.         ret = 342; /* minimum possible */
  260.     SetUpA4();
  261.     return(ret);
  262.     }
  263.  
  264. Boolean HasGetScreenHeight()
  265.     {
  266.     return(gacc.GetScreenHeight_Ext != NULL);
  267.     }
  268.  
  269. short GetScreenWidth()
  270.     {
  271.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  272.     short        ret;
  273.     
  274.     RestoreA4();
  275.     if (laccp->GetScreenWidth_Ext != NULL)
  276.         ret = (*(laccp->GetScreenWidth_Ext))();
  277.     else
  278.         ret = 512; /* minimum possible */
  279.     SetUpA4();
  280.     return(ret);
  281.     }
  282.  
  283. Boolean HasGetScreenWidth()
  284.     {
  285.     return(gacc.GetScreenWidth_Ext != NULL);
  286.     }
  287.  
  288. void SetWatchCursor()
  289.     {
  290.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  291.     
  292.     RestoreA4();
  293.     if (laccp->SetWatchCursor_Ext != NULL)
  294.         (*(laccp->SetWatchCursor_Ext))();
  295.     SetUpA4();
  296.     }
  297.  
  298. Boolean HasSetWatchCursor()
  299.     {
  300.     return(gacc.SetWatchCursor_Ext != NULL);
  301.     }
  302.  
  303. void DoEventLoopOnce()
  304.     {
  305.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  306.     
  307.     RestoreA4();
  308.     if (laccp->DoEventLoopOnce_Ext != NULL)
  309.         (*(laccp->DoEventLoopOnce_Ext))();
  310.     SetUpA4();
  311.     }
  312.  
  313. Boolean HasDoEventLoopOnce()
  314.     {
  315.     return(gacc.DoEventLoopOnce_Ext != NULL);
  316.     }
  317.  
  318. Handle GetTheClip()
  319.     {
  320.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  321.     Handle    ret;
  322.     
  323.     RestoreA4();
  324.     if (laccp->GetAppClip_Ext != NULL)
  325.         ret = (*(laccp->GetAppClip_Ext))();
  326.     else
  327.         ret = NULL;
  328.     SetUpA4();
  329.     return(ret);
  330.     }
  331.  
  332. Boolean HasGetTheClip()
  333.     {
  334.     return(gacc.GetAppClip_Ext != NULL);
  335.     }
  336.  
  337. // Note for version 3 the version is signalled by long extendID == 'VER3'
  338. // inside gacc -- for previous versions, the odds of this long being set
  339. // to exactly 'VER3' are very small.
  340.  
  341. short PutTheClip(char *newClipStr)
  342.     {
  343.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  344.     short    ret = 0;
  345.     
  346.     RestoreA4();
  347.     if (laccp->extendID == 'VER3' && laccp->PutAppClip_Ext != NULL)
  348.         ret = (*(laccp->PutAppClip_Ext))(newClipStr);
  349.     SetUpA4();
  350.     return(ret);
  351.     }
  352.  
  353. Boolean HasPutTheClip(void)
  354.     {
  355.     return(gacc.extendID == 'VER3' && gacc.PutAppClip_Ext != NULL);
  356.     }
  357.  
  358.